package top.saplf.sileny.player.home

import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.*
import me.eugeniomarletti.extras.bundle.BundleExtra
import me.eugeniomarletti.extras.bundle.base.Boolean
import me.eugeniomarletti.extras.bundle.base.Int
import me.eugeniomarletti.extras.bundle.base.String
import top.saplf.sileny.util.async

/**
 * @author saplf
 */
class PlayMusicService : Service() {

    companion object {
        var Bundle.musicPath: String by BundleExtra.String("")
        var Bundle.playProgress: Int by BundleExtra.Int(0)
        var Bundle.isPlaying: Boolean by BundleExtra.Boolean(false)

        const val CONNECTED = 6
        const val START = 0
        const val PAUSE = 1
        const val RESTART = 2
        const val STOP = 3
        const val SWITCH = 4

        const val PLAY_PROGRESS = 5
    }

    private val mMediaPlayer: MediaPlayer by lazy { MediaPlayer() }
    private val mHandlerThread: HandlerThread by lazy {
        HandlerThread(this::class.java.name).apply { start() }
    }
    private val mHandler: Handler by lazy {
        Handler(mHandlerThread.looper) {
            when (it.what) {
                CONNECTED -> mActivityMessenger = it.replyTo
                START -> {
                    if (it.data.musicPath == mCurrentMusic) {
                        mMediaPlayer.start()
                    } else {
                        mMediaPlayer.reset()
                        mCurrentMusic = it.data.musicPath
                        mMediaPlayer.setDataSource(mCurrentMusic)
                        mMediaPlayer.prepare()
                        mMediaPlayer.start()
                    }
                    sendMessage(true)
                }
                PAUSE -> {
                    if (mMediaPlayer.isPlaying) {
                        mMediaPlayer.pause()
                        sendMessage(false)
                    }
                }
                RESTART -> {
                    mMediaPlayer.start()
                }
                STOP -> {
                    mMediaPlayer.stop()
                }
                SWITCH -> {
                }
            }
            true
        }
    }
    private val mServiceMessenger: Messenger by lazy { Messenger(mHandler) }
    private var mActivityMessenger: Messenger? = null
    private var mCurrentMusic: String = ""

    override fun onBind(intent: Intent?): IBinder? = mServiceMessenger.binder

    private fun sendMessage(playing: Boolean) {
        if (playing) {
            async {
                while (mMediaPlayer.isPlaying) {
                    Message.obtain().apply {
                        what = PLAY_PROGRESS
                        data = Bundle().apply {
                            playProgress = mMediaPlayer.currentPosition
                            isPlaying = true
                        }
                        mActivityMessenger?.send(this)
                    }
                    Thread.sleep(100)
                }
            }
        } else {
            Message.obtain().apply {
                what = PLAY_PROGRESS
                data = Bundle().apply {
                    playProgress = mMediaPlayer.currentPosition
                    isPlaying = false
                }
                mActivityMessenger?.send(this)
            }
        }
    }
}